﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Pur;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Pur
{
    public partial class frmMaintainPurchaseOrderQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        private bool mMultiSelected = false;
        private List<PurPurchaseOrderEntity> mSelectedPurchaseOrders;
        public List<PurPurchaseOrderEntity> SelectedPurchaseOrders
        {
            get { return mSelectedPurchaseOrders; }
        }
        private PurPurchaseOrderEntity mSelectedPurchaseOrder;
        public PurPurchaseOrderEntity SelectedPurchaseOrder
        {
            get { return mSelectedPurchaseOrder; }
        }
        #endregion

        #region Constructor
        public frmMaintainPurchaseOrderQuery(bool multiSelected)
        {
            InitializeComponent();
            mMultiSelected = multiSelected;
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "PURST", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCURRENCY, "CS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "PURST", true);
        }

        private string GetCondition()
        {
            string condition = "";
            if (txtSupplier.Tag != null && txtSupplier.Tag.GetType() == typeof(MstSupplierEntity))
            {
                MstSupplierEntity supplier = (MstSupplierEntity)txtSupplier.Tag;
                condition += " AND PO.SUPPLIER_ID = '" + supplier.SupplierId + "' ";
            }
            if (txtPurchasePerson.Tag != null && txtPurchasePerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtPurchasePerson.Tag;
                condition += " AND PO.PURCHASE_PERSON = '" + entity.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND PO.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            if (dtpPurchaseDate.EditValue!=null)
            {
                condition += " AND PO.PURCHASE_DATE >= CONVERT(DATETIME,'" + ((DateTime)dtpPurchaseDate.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (dtpPurchaseDateTo.EditValue != null)
            {
                condition += " AND PO.PURCHASE_DATE <= CONVERT(DATETIME,'" + ((DateTime)dtpPurchaseDateTo.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (dtpDeliveryRequiredDate.EditValue != null)
            {
                condition += " AND PO.DELIVERY_REQUIRED_DATE >= CONVERT(DATETIME,'" + ((DateTime)dtpDeliveryRequiredDate.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (dtpDeliveryRequiredDateTo.EditValue != null)
            {
                condition += " AND PO.DELIVERY_REQUIRED_DATE <= CONVERT(DATETIME,'" +((DateTime) dtpDeliveryRequiredDateTo.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (cboxOnlyShowNonArrived.Checked)
            {
                condition += " AND X.PURCHASE_ORDER_ID IS NULL ";
            }
            if (string.IsNullOrEmpty(txtPurchaseOrder.Text) == false)
            {
                condition += " AND PO.PURCHASE_ORDER_CODE LIKE '%" + txtPurchaseOrder.Text + "%' ";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "PO.PURCHASE_ORDER_CODE DESC";
            DataTable dt = PurPurchaseOrderBs.GetByCondition(condition, "", orderBy, "","");
            DataColumn dc = new DataColumn("IS_SELECTED", typeof(bool));
            dc.DefaultValue = false;
            dt.Columns.Add(dc);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            if (mMultiSelected == false)
            {
                gridView1.Columns["IS_SELECTED"].Visible = false;
            }
            base.OnShown(e);
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Confirm()
        {
            if (gvMain.DataSource == null)
            {
                return;
            }
            if (mMultiSelected)
            {
                DataTable dt = gvMain.DataSource as DataTable;
                mSelectedPurchaseOrders = new List<PurPurchaseOrderEntity>();
                foreach (DataRow dr in dt.Rows)
                {
                    if ((bool)dr["IS_SELECTED"])
                    {
                        PurPurchaseOrderEntity entity = new PurPurchaseOrderEntity(dr);
                        mSelectedPurchaseOrders.Add(entity);
                    }
                }
                if (mSelectedPurchaseOrders == null || mSelectedPurchaseOrders.Count == 0)
                {
                    MessageBox.Show("请选择一个采购订单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                if (mSelectedPurchaseOrder == null)
                {
                    MessageBox.Show("请选择一个采购订单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    this.DialogResult = DialogResult.OK;
                }
            }
        }

        private void txtSupplier_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainSupplierQuery frm = new frmMaintainSupplierQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedSupplier != null)
            {
                txtSupplier.Text = frm.SelectedSupplier.SupplierName;
                txtSupplier.Tag = frm.SelectedSupplier;
            }
            else
            {
                txtSupplier.Text = "";
                txtSupplier.Tag = null;
            }
        }

        private void txtPurchasePerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtPurchasePerson.Text = frm.SelectedPerson.NameCn;
                txtPurchasePerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtPurchasePerson.Text = "";
                txtPurchasePerson.Tag = null;
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedPurchaseOrder = new PurPurchaseOrderEntity(dr);
            if ((bool)dr["IS_SELECTED"])
            {
                dr["IS_SELECTED"] = false;
            }
            else
            {
                dr["IS_SELECTED"] = true;
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (mMultiSelected == false)
            {
                if (mSelectedPurchaseOrder == null)
                {
                    MessageBox.Show("请选择一个采购订单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    this.DialogResult = DialogResult.OK;
                }
            }
        }
        #endregion
    }
}
